jsx3.require("jsx3.gui.Template");

jsx3.lang.Class.defineClass("ux.core.gui.TextFieldFormRow", jsx3.gui.Template.Block, null,
    function(TEXTFIELD, textfield) {

        textfield.iconPath = ux.core.System.getServer().resolveURI('jsxaddin://user!LiveCore/images/matrix/info.gif');
		jsx3.log("info icon path : " + textfield.iconPath);
        textfield.fieldtitletext = ux.core.getDynamicProperty("uxcore@formrow@defaultTitle");
        textfield.fieldinstructionstext = null;
        textfield.fieldexampletext = null;
        textfield.fielderrortext = null;

        textfield.init = function(strName) {
            this.jsxsuper(strName);
        }

        textfield.onSetParent = function(objParent) {
            var ret = this.jsxsuper(objParent);
            if(objParent.instanceOf("ux.core.gui.FormPanel")) {
                delete this.fielderrortext;
                delete this.fieldvalue;
                delete this.wastab;
                delete this.wasshift;
                delete this.hasfocus;
            }
            else {
                ret = false;
            }
            return ret;
        }

        textfield.setTitleText = function(strText, bRepaint) {
            this.setProperty("fieldtitletext", strText);
            if(bRepaint) {
                this.repaint();
            }
            return this;
        }

        textfield.getTitleText = function() {
            return this.fieldtitletext;
        }

        textfield.setInstructionsText = function(strText, bRepaint) {
            this.setProperty("fieldinstructionstext", strText);
            if(bRepaint) {
                this.repaint();
            }
            return this;
        }

        textfield.getInstructionsText = function() {
            return this.fieldinstructionstext;
        }

        textfield.setExampleText = function(strText, bRepaint) {
            this.setProperty("fieldexampletext", strText);
            if(bRepaint) {
                this.repaint();
            }
            return this;
        }

        textfield.getExampleText = function() {
            return this.fieldexampletext;
        }

        textfield.setErrorText = function(strText, bRepaint) {
            this.setProperty("fielderrortext", strText);
            if(bRepaint) {
                this.repaint();
            }
            return this;
        }

        textfield.getErrorText = function() {
            return this.fielderrortext;
        }

        textfield.setValue = function(strValue) {
            if(strValue && (ux.core.util.Validation.trimAll(strValue) == "")) {
                strValue = null;
            }
            this.setProperty("fieldvalue", strValue);
            this.validate();
            return this;
        }

        textfield.validate = function() {
            var bRepaint = false;
            if(this.validator) {
                if(this.validator == "@noWhitespace") {
                    if(this.isrequired == jsx3.Boolean.TRUE) {
                        bRepaint = ux.core.util.Validation.doValidateNotEmptyNoWhSpace(this);
                    }
                    else {
                        bRepaint = ux.core.util.Validation.doValidateNoWhSpace(this);
                    }
                }
                else {
                    if(this.isrequired == jsx3.Boolean.TRUE) {
                        bRepaint = ux.core.util.Validation.doValidateNotEmpty(this);
                    }
                    if(bRepaint) {
                    var result = jsx3.eval(this.validator,{objJSX:this, value:this.getValue()});
                    var valid = false;
                    var errorText = ux.core.getDynamicProperty("uxcore@validation@field@defaultError");
                    var type = typeof(result);
                    switch(type) {
                        case "boolean":
                           valid = result;
                           break;
                        case "number":
                           valid = (result == jsx3.Boolean.TRUE)
                           break;
                        case "string":
                            var lower = result.toLowerCase();
                            valid = (lower == "true");
                            if(!valid && (lower != "false")) {
                                errorText = result;
                            }
                            break;
                        default:
                    }
                        this.setErrorText(valid ? null : errorText, true);
                        bRepaint = false;
                    }
                    if(bRepaint) {
                        this.repaint();
                    }
                }
            }
            else if(this.isrequired == jsx3.Boolean.TRUE) {
                bRepaint = ux.core.util.Validation.doValidateNotEmpty(this);
            }
            else if(this.fielderrortext) {
                this.setErrorText(null, true);
            }
            if(bRepaint) {
                this.repaint();
            }
        }

        textfield.getValue = function() {
            return this.fieldvalue;
        }

        textfield.setCDFAttribute = function(strAttName) {
            this.setProperty("cdfattribute", strAttName);
        }

        textfield.getCDFAttribute = function() {
            return this.cdfattribute;
        }

        textfield.setFieldValidator = function(strAttName) {
            this.setProperty("validator", strAttName);
        }

        textfield.getFieldValidator = function() {
            return this.validator;
        }

        textfield.setRequired = function(intRequired, bRepaint) {
            this.setProperty("isrequired", intRequired);
            if(bRepaint) {
                this.repaint();
            }
        }

        textfield.isRequired = function() {
            return this.isrequired;
        }

        textfield.focus = function() {
            this.getRenderedBox("textfield").style.border = ux.core.getDynamicProperty("uxcore@formrow@activeBorder");
            this.getRenderedBox("textfield").focus();
        }

        textfield.onkeydown = function(objEvent, objGUI) {
            this.wastab = objEvent.tabKey();
            this.wasshift = objEvent.shiftKey()
        }

        textfield.onmousedown = function() {
            this.focus();
        }

        textfield.onblur = function(objEvent, objGUI) {
            this.setValue(objGUI.value);
            this.getRenderedBox("textfield").style.border = ux.core.getDynamicProperty("uxcore@formrow@inactiveBorder");
            if(this.wastab) {
                this.wastab = false;
                var sib = this.wasshift ? this.getParent().getPreviousFormRow(this) : this.getParent().getNextFormRow(this);
                this.wasshift = false;
                sib.focus();
            }
            return true;
        }

        TEXTFIELD.templateXML = ux.core.System.getServer().loadInclude("jsxaddin://user!LiveCore/classes/ux/core/gui/templates/formrow.xml","textFieldFormRowTemplate","xml").toString();
//        jsx3.log(TEXTFIELD.templateXML);
        jsx3.gui.Template.compile(TEXTFIELD.templateXML,TEXTFIELD.jsxclass);
//        jsx3.gui.Template.compile('<transform xmlns="http://gi.ux.com/transform/" xmlns:u="http://gi.ux.com/transform/user" version="1.0"><template onpaint="this.setValue(this.getValue());"><inlinebox class="jsx30slider" index="{$index}" jsxindex="{$jsxindex}" label="{$label}" title="{$tip}" style="width:100%;display:{$display}; margin: 0px 0px 10px;"><div style="border: 0px none; margin: 0px; padding: 0px; position: relative; width: 100%; font-size: 11px; color: #000000; font-family: Arial; font-weight: bold; display: block; float: left;">[Label Text]</div><div style="border: 0px none; margin: 5px 0px 0px; padding: 0px; position: relative; width: 100%; display: block; float: left;"><table width="100%" cellspacing="0" cellpadding="0"><tbody><tr><td valign="top" height="16"><span style="margin-left: 5px; font-family: Arial; font-size: 11px; font-weight: bold; color: #7A7A7A;">Field Instructions</span></td></tr></tbody></table></div><input type="text" style="border: 1px solid rgb(85, 85, 85); margin: 5px 0px 0px; padding: 1px 5px 0px; position: relative; width: 100%; height: 20px; left: 0px; top: 0px; font-family: Arial; font-size: 11px; font-weight: normal; background-color: #FFFFFF; float: left;" value="" onblur="jsx3.dl(event,this,&quot;ah&quot;,0);"/></inlinebox></template></transform>',TEXTFIELD.jsxclass);
    }
);